package me.zhengjie.service.impl;

import lombok.RequiredArgsConstructor;
import me.zhengjie.pojo.dto.AgvRequestLiftDto;
import me.zhengjie.pojo.dto.LiftRequestDto;
import me.zhengjie.pojo.entity.LiftTask;
import me.zhengjie.pojo.entity.LiftTaskHistory;
import me.zhengjie.pojo.entity.LiftWaitPoint;
import me.zhengjie.pojo.vo.AgvTaskSubmitVo;
import me.zhengjie.service.AgvReturnService;
import me.zhengjie.sqlservilce.LiftTaskHistoryService;
import me.zhengjie.sqlservilce.LiftTaskSqlService;
import me.zhengjie.sqlservilce.LiftWaitPointService;
import me.zhengjie.util.state.LiftTaskStatus;
import me.zhengjie.util.state.TaskType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
@RequiredArgsConstructor
public class AgvReturnServiceImpl implements AgvReturnService {
    private static final Logger haikang = LoggerFactory.getLogger("haiKang");

    private final LiftWaitPointService liftWaitPointService;

    private final LiftTaskSqlService liftTaskSqlService;

    private final me.zhengjie.service.LiftTaskService liftTaskService;

    private final LiftTaskServiceImpl liftTaskServiceImpls;

    private final LiftTaskHistoryService liftTaskHistoryService;



    @Transactional
    @Override
    public AgvTaskSubmitVo agvRequestLift(AgvRequestLiftDto agvRequestLiftDto) throws Exception {
        haikang.info("请求电梯请求参数：{}",agvRequestLiftDto);
        //设备编号
        String eqptCode = agvRequestLiftDto.getEqptCode();
        //设备名称
//        String eqptName = agvRequestLiftDto.getEqptName();
        //任务号为唯一编号，可使用 UUID
        String taskCode = agvRequestLiftDto.getTaskCode();
        //任务执行方法，与设备相关，可扩展枚举值。
        String method = agvRequestLiftDto.getMethod();
        //起点
        String srcCode = agvRequestLiftDto.getSrcCode();
        //终点
        String dstCode = agvRequestLiftDto.getDstCode();

        if (LiftTaskStatus.APPLY_RESOURCE.getMessage().equals(method)){
            if (TaskType.NEW_FLOOR.getMessage().equals(eqptCode)){
                LiftWaitPoint liftWaitPoint = new LiftWaitPoint();
                liftWaitPoint.setLiftCode(eqptCode);
                liftWaitPoint.setTaskCode(taskCode);
                liftWaitPoint.setStartFloor(srcCode);
                liftWaitPoint.setEndFloor(dstCode);
                liftWaitPoint.setCreateTime(LocalDateTime.now());
                //电梯任务创建
                boolean b = liftWaitPointService.insertLiftWaitPoint(liftWaitPoint);
                if (!b){
                    haikang.error("创建电梯等待点失败，数据库异常");
                    throw new RuntimeException("创建电梯等待点失败，数据库异常");
                }
                return AgvTaskSubmitVo.OK(null);
            }else if (TaskType.PAST_FLOOR.getMessage().equals(eqptCode)){
                //获取token，发送给旺龙
                String toke = liftTaskService.developerLogin(taskCode);
                //下发电梯任务
                LiftRequestDto liftRequestDto = new LiftRequestDto();
                liftRequestDto.setDecrypt(toke);
                liftRequestDto.setRobotId(TaskType.PAST_FLOOR.getMessage());
                liftRequestDto.setRobotId(TaskType.PAST_FLOOR.getMessage());
                liftRequestDto.setFromFloor(srcCode);
                liftRequestDto.setToFloor(dstCode);
                liftRequestDto.setOpenTime("30");
                String requestId = liftTaskService.getRequestId();
                liftRequestDto.setRequestId(requestId);
                String s = liftTaskService.callElevator(liftRequestDto);
                //存数据库
                LiftTask liftTask = new LiftTask();
                liftTask.setTaskCode(taskCode);
                liftTask.setWangLongTaskIId(requestId);
                liftTask.setLiftCode(eqptCode);
                liftTask.setStartFloor(srcCode);
                liftTask.setEndFloor(dstCode);
                liftTask.setStatus("0");
                liftTask.setCreateTime(LocalDateTime.now());
                liftTask.setUpdateTime(LocalDateTime.now());
                liftTask.setLiftStorage("电梯储位");
                liftTaskSqlService.insertLiftTask(liftTask);
            }
        }else if (LiftTaskStatus.EXECUTE_TASK.getMessage().equals(method)){
            LiftTask liftTask = liftTaskSqlService.selectLIftTask(taskCode);
            if (liftTask !=null){
                //AGV上报到达电梯储位，修改电梯任务状态
                boolean b1 = liftTaskSqlService.updateStatus(liftTask.getId(), LiftTaskStatus.ARRIVE_STORAGE_POINT.getCode());
                if (!b1){
                    haikang.error("修改电梯任务状态为4，数据库异常，回滚");
                    throw new RuntimeException("修改电梯任务状态为4，数据库异常，回滚");
                }
                if (TaskType.PAST_FLOOR.getMessage().equals(eqptCode)){
                    //获取token，发送给旺龙
                    String toke = liftTaskService.developerLogin(taskCode);
                    LiftRequestDto liftRequestDto = new LiftRequestDto();
                    liftRequestDto.setRobotId(eqptCode);
                    liftRequestDto.setDecrypt(toke);
                    liftRequestDto.setPosition("out");
                    liftRequestDto.setOpenTime("20");
                    String s = liftTaskServiceImpls.sendOpenDoor(liftRequestDto);
                    liftTaskSqlService.updateStatus(liftTask.getId(), LiftTaskStatus.CLOSE_DOOR_TO_END_FLOOR.getCode());
                }
                return AgvTaskSubmitVo.OK(null);
            }
            return AgvTaskSubmitVo.OK(taskCode);
        }else if (LiftTaskStatus.RELEASE_RESOURCE.getMessage().equals(method)){
            //AGV上报离开电梯储位，修改电梯任务状态
            LiftTask liftTask = liftTaskSqlService.selectLIftTask(taskCode);
            if (liftTask !=null){
                boolean b1 = liftTaskSqlService.updateStatus(liftTask.getId(), LiftTaskStatus.LEAVE_LIFT.getCode());
                if (!b1){
                    haikang.error("修改电梯任务状态为8，数据库异常，回滚");
                    throw new RuntimeException("修改电梯任务状态为8，数据库异常，回滚");
                }
                if (TaskType.PAST_FLOOR.getMessage().equals(eqptCode)){
                    //获取token，发送给旺龙
                    String toke = liftTaskService.developerLogin(taskCode);
                    LiftRequestDto liftRequestDto = new LiftRequestDto();
                    liftRequestDto.setRobotId(eqptCode);
                    liftRequestDto.setDecrypt(toke);
                    liftRequestDto.setPosition("in");
                    liftRequestDto.setOpenTime("20");
                    liftTaskServiceImpls.sendOpenDoor(liftRequestDto);
                    String s = liftTaskServiceImpls.sendOpenDoor(liftRequestDto);
                    liftTaskSqlService.updateStatus(liftTask.getId(), LiftTaskStatus.TO_CLOSE_DOOR.getCode());

                    LiftTaskHistory liftTaskHistory = new LiftTaskHistory();
                    BeanUtils.copyProperties(liftTask,liftTaskHistory);
                    liftTaskHistory.setId(null);
                    boolean b = liftTaskHistoryService.insertLiftTask(liftTaskHistory);
                    if (!b){
                        throw new RuntimeException("将任务添加到历史表失败，数据回滚");
                    }
                    //删除电梯任务
                    boolean b2 = liftTaskSqlService.deleteId(liftTask.getId());
                    if (!b2){
                        throw new RuntimeException("删除任务失败");
                    }
                }
            }
            return AgvTaskSubmitVo.OK(null);
        }
        return AgvTaskSubmitVo.ng(taskCode);
    }


}
